/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerConfBookTour", urlPatterns = {"/ControllerConfBookTour"})
public class ControllerConfBookTour extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       Date date=new Date();
        
        SimpleDateFormat simpleDate=new SimpleDateFormat("MM/dd/yyyy");
        
        String strdate=simpleDate.format(date);
        HttpSession session=request.getSession(true);
        
        String idtour=request.getParameter("idtour");
        String username=(String) session.getAttribute("User");
        String nametour=request.getParameter("nametour");
        String priceAdu=request.getParameter("ap");
        String priceChil=request.getParameter("cp");
        String numberOfAdu=request.getParameter("opadult");
        String numberOfchild=request.getParameter("opchild");
        String startDate=request.getParameter("startdate");
        String endDate=request.getParameter("enddate");
        String cash=request.getParameter("methodPay");
        
        int numOfchild=Integer.parseInt(numberOfchild);
        int numOfAdu=Integer.parseInt(numberOfAdu);
        
        float pAdul=Float.parseFloat(priceAdu);
        float pchil=Float.parseFloat(priceChil);
        
        float mchil=numOfchild*pchil;
        float ma=numOfAdu*pAdul;
        
        int sumGuest=numOfchild + numOfAdu;
        float sumMoney=mchil+ma;
        
        StringTokenizer t = new StringTokenizer(startDate, "-");
        String datee = t.nextToken();
        int daySt=Integer.parseInt(datee);
        String month = t.nextToken();
        int monthSt=Integer.parseInt(month);
        String year = t.nextToken();
        String dateStart=month+"/"+datee+"/"+year;
        
        StringTokenizer e = new StringTokenizer(endDate, "-");
        String d = e.nextToken();
        int dEnd=Integer.parseInt(d);
        String m = e.nextToken();
        int mEnd=Integer.parseInt(m);
        String y = e.nextToken();
        String dateEnd=m +"/"+d +"/"+y;
        
      if(daySt==dEnd && monthSt==mEnd)
        {
            out.println("<script>alert('Please choose Start day of traveling less than End day of traveling');javascript:window.history.back(-1);</script>");
  
        }
        else if(daySt > dEnd && monthSt == mEnd) {
            out.println("<script>alert('Please choose End day of traveling greater than Start day of traveling 2');javascript:window.history.back(-1);</script>");
        }
        else if(monthSt > mEnd) {
            out.println("<script>alert('Please choose Start month of traveling less than End month of traveling');javascript:window.history.back(-1);</script>");
        } else {
            session.setAttribute("Cbidtour",idtour);
            session.setAttribute("Cbnametour",nametour);
            session.setAttribute("CbpriceAdu",priceAdu);
            session.setAttribute("CbpriceChil",priceChil);
            session.setAttribute("CbnumberOfAdu",numberOfAdu);
            session.setAttribute("CbnumberOfchild",numberOfchild);
            session.setAttribute("CbstartDate",startDate);
            session.setAttribute("CbendDate",endDate);
            session.setAttribute("Cbcash",cash);
            session.setAttribute("CbsumMoney",sumMoney);
            session.setAttribute("CbsumGuest",sumGuest);
            response.sendRedirect("index.jsp?page=ConfirmBookTour");
       }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
